<?php
/**
 * @name: HOTN Login Menu
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnloginmenu/
 */ 

if (!defined('e107_INIT')) { exit; }

global $tp;
@include(e_PLUGIN."login_menu/languages/".e_LANGUAGE.".php");
require_once(e_PLUGIN."hotn_login_menu/hotn_login_menu_shortcodes.php");
if (file_exists(THEME."hotn_login_menu/hotn_login_menu_template.php"))
	require_once(THEME."hotn_login_menu/hotn_login_menu_template.php");
else
	require_once(e_PLUGIN."hotn_login_menu/hotn_login_menu_template.php");

if (USER)
{
	
	$MAIN =  $tp->parseTemplate($HOTN_LOGIN_AVATAR_TEMPLATE, false, $hotn_login_menu_shortcodes);
}
else
{
	
	$MAIN =  $tp->parseTemplate($HOTN_LOGIN_LOGIN_TEMPLATE, false, $hotn_login_menu_shortcodes);
}

global $HOTN_LOGIN_MAIN, $HOTN_LOGIN_PRIVATE, $HOTN_LOGIN_AVATAR, $HOTN_LOGIN_PM, $HOTN_LOGIN_PM_INBOX_ALL; 
global $HOTN_LOGIN_PM_INBOX_UNREAD, $HOTN_LOGIN_PM_OUTBOX_ALL, $HOTN_LOGIN_PM_OUTBOX_UNREAD, $HOTN_LOGIN_USER_NAME;
global $HOTN_LOGIN_ONLINE, $HOTN_LOGIN_ONLINE_USER, $HOTN_LOGIN_PAGE;
$HOTN_LOGIN_MAIN = "";
$HOTN_LOGIN_PRIVATE = "";
$HOTN_LOGIN_AVATAR = "";
$HOTN_LOGIN_PM = "";
$HOTN_LOGIN_PM_INBOX_ALL = 0;
$HOTN_LOGIN_PM_INBOX_UNREAD = 0;
$HOTN_LOGIN_PM_OUTBOX_ALL = 0;
$HOTN_LOGIN_PM_OUTBOX_UNREAD = 0;
$HOTN_LOGIN_USER_NAME = "";
$HOTN_LOGIN_ONLINE = "";
$HOTN_LOGIN_ONLINE_USER = "";
$HOTN_LOGIN_PAGE = "";

if (USER == FALSE)
{
	// Nincs belépve
	$caption = HOTN_LOGIN_MENU_CAPTION.USERNAME;
	
	// MAIN
	$HOTN_LOGIN_MAIN .= '<form method="post" action="'.e_SELF.(e_QUERY ? '?'.e_QUERY : '').'">';
	$HOTN_LOGIN_MAIN .=  $tp->parseTemplate($HOTN_LOGIN_LOGIN_TEMPLATE, false, $hotn_login_menu_shortcodes);
	$HOTN_LOGIN_MAIN .= '</form>';
	
	// PM
	//------
	
	
}
else
{
	// Be van lépve
	$caption = HOTN_LOGIN_MENU_CAPTION_GUEST;
	
	// MAIN
	$sql = new db;
	if($sql -> db_Select("user", "*", "user_id='".USERID."'"))
	{
		$row = $sql -> db_Fetch();
		if($row[user_image] == "") 
		{
			if (file_exists(THEME."hotn_login_menu/hotn_login_default.png"))
				$HOTN_LOGIN_AVATAR = THEME."hotn_login_menu/hotn_login_default.png";
			else
				$HOTN_LOGIN_AVATAR = e_PLUGIN."hotn_login_menu/images/hotn_login_default.png";
		}
		else
		{
			require_once(e_HANDLER."avatar_handler.php");
			$HOTN_LOGIN_AVATAR = avatar($row['user_image']);
		}
	}
	
	$HOTN_LOGIN_MAIN = $tp->parseTemplate($HOTN_LOGIN_AVATAR_TEMPLATE, false, $hotn_login_menu_shortcodes);
	
	// PM
	if ($pref['plug_installed']['pm']<>"")
	{
		include_once(e_PLUGIN."hotn_login_menu/languages/".e_LANGUAGE.".php");
		
		// Inbox icon
			if (file_exists(THEME."hotn_login_menu/images/hotn_login_inbox.gif"))
			{
				define("HOTN_LOGIN_PM_INBOX_ICON",THEME."hotn_login_menu/images/hotn_login_inbox.gif");
			}
			else
			{
				define("HOTN_LOGIN_PM_INBOX_ICON",e_PLUGIN."hotn_login_menu/images/hotn_login_inbox.gif");
			}
		
		// Outbox icon
			if (file_exists(THEME."hotn_login_menu/images/hotn_login_outbox.gif"))
			{
				define("HOTN_LOGIN_PM_OUTBOX_ICON",THEME."hotn_login_menu/images/hotn_login_outbox.gif");
			}
			else
			{
				define("HOTN_LOGIN_PM_OUTBOX_ICON",e_PLUGIN."hotn_login_menu/images/hotn_login_outbox.gif");
			}
		
		// New icon
			if (file_exists(THEME."hotn_login_menu/images/hotn_login_newpm.gif"))
			{
				define("HOTN_LOGIN_PM_NEWPM_ICON",THEME."hotn_login_menu/images/hotn_login_newpm.gif");
			}
			else
			{
				define("HOTN_LOGIN_PM_NEWPM_ICON",e_PLUGIN."hotn_login_menu/images/hotn_login_newpm.gif");
			}
		
		//Adatok gyüjtése
			require_once(e_PLUGIN."pm/pm_func.php");
			pm_getInfo('clear');
			
			$HOTN_LOGIN_PM_INBOX = pm_getInfo("inbox");
			$HOTN_LOGIN_PM_INBOX_ALL = $HOTN_LOGIN_PM_INBOX['inbox']['total'];
			$HOTN_LOGIN_PM_INBOX_UNREAD = $HOTN_LOGIN_PM_INBOX['inbox']['unread']; 
			$HOTN_LOGIN_PM_OUTBOX = pm_getInfo("outbox");
			$HOTN_LOGIN_PM_OUTBOX_ALL = $HOTN_LOGIN_PM_OUTBOX['outbox']['total'];
			$HOTN_LOGIN_PM_OUTBOX_UNREAD = $HOTN_LOGIN_PM_OUTBOX['outbox']['unread'];
			
		$HOTN_LOGIN_PM = $tp->parseTemplate($HOTN_LOGIN_PM_TEMPLATE,false,$hotn_login_menu_shortcodes);

		// OnLine
			$sql->db_Select("online, ".MPREFIX."user", "*", "online_user_id!='0' and user_id=SUBSTRING_INDEX(`online_user_id`,'.',1) ORDER BY `user_name` ASC","default");
            while ($row = $sql->db_Fetch())
            {
            	$isadmin = $row['user_admin'];
	            if($row[user_image] == "") 
				{
					if (file_exists(THEME."hotn_login_menu/hotn_login_default.png"))
						$HOTN_LOGIN_AVATAR = THEME."hotn_login_menu/hotn_login_default.png";
					else
						$HOTN_LOGIN_AVATAR = e_PLUGIN."hotn_login_menu/images/hotn_login_default.png";
				}
				else
				{
					require_once(e_HANDLER."avatar_handler.php");
					$HOTN_LOGIN_AVATAR = avatar($row['user_image']);
				}
				
				$HOTN_LOGIN_AVATAR = "<a href='".e_BASE."user.php?id.".$row['user_id']."'><img src='".$HOTN_LOGIN_AVATAR."' width='25' /></a>";
				$HOTN_LOGIN_USER_NAME = "<a href='".e_BASE."user.php?id.".$row['user_id']."'>".$row['user_name']."</a>";
				
				$online_location_page = eregi_replace(".php", "", substr(strrchr($row['online_location'], "/"), 1));
            	if ($online_location_page == "log" || $online_location_page == "error")
                {
                    $online_location = "news.php";
                    $online_location_page = HOTN_LOGIN_ONLINE_NEWS;
                } 
                if ($online_location_page == "request" || $online_location_page == "project" || $online_location_page == "project_view")
                {
                    $online_location = e_PLUGIN."hotn_animelist/project.php";
                    $online_location_page = HOTN_LOGIN_ONLINE_PROJECT;
                } 
                if (strstr($online_location_page, "forum"))
                {
					$online_location = e_PLUGIN."forum/forum.php";
                    $online_location_page = HOTN_LOGIN_ONLINE_FORUM;
                } 
                if ($row['isadmin'])
                {
                    $online_location = "news.php";
                    $online_location_page = HOTN_LOGIN_ONLINE_NEWS;
                }
                
                $HOTN_LOGIN_PAGE = "<a href='".$online_location."'>".$online_location_page."</a>";
                
                
				
				$HOTN_LOGIN_ONLINE_USER[] = $tp->parseTemplate($HOTN_ONLINE_USER_TEMPLATE, false, $hotn_login_menu_shortcodes);
            }
            $HOTN_LOGIN_ONLINE_USER = implode("<br />",$HOTN_LOGIN_ONLINE_USER);
		
			$HOTN_LOGIN_ONLINE = $tp->parseTemplate($HOTN_ONLINE_TEMPLATE, false, $hotn_login_menu_shortcodes);
	}
}

$text = $tp->parseTemplate($HOTN_LOGIN_MENU_TEMPLATE, false, $hotn_login_menu_shortcodes);


$ns->tablerender($caption, $text, 'hotn_login');
?>